<template>
  <div>
    <h1>手写isEqual函数(比较数组，对象里的值是否相等，返回true,false)</h1>
  </div>
</template>

<script setup lang="ts">
let data1 = {
  a: 100,
  b: 200,
  c: {
    z: 'a'
  }
}
let data2 = {
  a: 100,
  b: 200,
  c: {
    z: 'a'
  }
}
let data3 = 123

console.log(data1 === data2, '11111111111')
console.log(isEqual(data1, data2), '22222222222')

function isEqual(data1: any, data2: any) {
  // 1.先判断是否是引用数据类型
  if ((typeof data1 !== 'object' && data1 !== null) || (typeof data2 !== 'object' && data2 !== null)) {
    // 值类型
    return data1 === data2
  }
  //   2.判断传参进来的是否是一样的
  if (data1 === data2) {
    return true
  }
  //   3.以data1为基准，比较数组和对象
  if (Array.isArray(data1) !== Array.isArray(data2)) {
    return false
  }
  //   4.比较长度是否一致
  if (Array.isArray(data1)) {
    if (data1.length !== data2.length) {
      return false
    }
  } else {
    // 对象  获取key
    let objKey1 = Object.keys(data1)
    let objKey2 = Object.keys(data2)
    if (objKey1.length !== objKey2.length) {
      return false
    }
  }
  for (const key in data1) {
    console.log(data1[key], data2[key], '壁纸ui')

    let res = isEqual(data1[key], data2[key])
    if (!res) {
      return false
    }
  }
  return true
}

let p = [1, 2, 3, 4] as any
let res2 = p.splice(2, 1, 'a')
console.log(p, res2)
</script>

<style scoped></style>
